package com.grasp.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.grasp.base.Logger;
import com.grasp.data.IconItem;

/**
 * 区域表
 * 
 */
public class AreaTable implements SQLiteInterface {

	private static final int COLUMN0 = 0;
	private static final int COLUMN1 = 1;
	private static final int COLUMN2 = 2;
	private static final int COLUMN3 = 3;

	private MySQLiteHelper mMySQLiteHelper;
	private SQLiteDatabase mSQLiteDatabase;

	public static final String AREA_TABLE = "AreaTable";
	private static final String TABLE_ID = "_id";
	private static final String AREA_NAME = "area_name";
	private static final String AREA_ICON_NAME = "icon_name";
	private static final String AREA_DESC = "_describe";
	private static final String AREA_COLUMNS[] = { TABLE_ID, AREA_NAME,
			AREA_ICON_NAME, AREA_DESC };

	public AreaTable(Context context) {
		mMySQLiteHelper = new MySQLiteHelper(context, this);
		mSQLiteDatabase = mMySQLiteHelper.getWritableDatabase();
	}

	@Override
	public void createTable(SQLiteDatabase db) {
		String areaSql = "CREATE TABLE IF NOT EXISTS " + AREA_TABLE + " ("
				+ AREA_COLUMNS[COLUMN0]
				+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ AREA_COLUMNS[COLUMN1] + " VARCHAR, " + AREA_COLUMNS[COLUMN2]
				+ " VARCHAR, " + AREA_COLUMNS[COLUMN3] + " VARCHAR);";
		Logger.d("===>areaSql:" + areaSql);
		Logger.d("===>mSQLiteDatabase:" + mSQLiteDatabase);
		db.execSQL(areaSql);
	}

	@Override
	public void onUpgrade(int oldVersion, int newVersion) {

	}

	public boolean insert(IconItem item) {
		ContentValues values = new ContentValues();
		values.put(AREA_COLUMNS[COLUMN1], item.getTitle());
		values.put(AREA_COLUMNS[COLUMN2], item.getFileName());
		values.put(AREA_COLUMNS[COLUMN3], item.getDesc());
		long number = mSQLiteDatabase.insert(AREA_TABLE, null, values);
		Logger.i("===>insert:" + number);
		return number == 1 ? true : false;
	}

	public boolean delete(IconItem item) {
		String[] whereArgs = { String.valueOf(item.getId()) };
		int number = mSQLiteDatabase.delete(AREA_TABLE, AREA_COLUMNS[COLUMN0]
				+ "=?", whereArgs);
		return number == 1 ? true : false;
	}

	public boolean update(IconItem item) {
		ContentValues values = new ContentValues();
		values.put(AREA_COLUMNS[COLUMN1], item.getTitle());
		values.put(AREA_COLUMNS[COLUMN2], item.getFileName());
		values.put(AREA_COLUMNS[COLUMN3], item.getDesc());
		String[] whereArgs = { String.valueOf(item.getId()) };
		int number = mSQLiteDatabase.update(AREA_TABLE, values,
				AREA_COLUMNS[COLUMN0] + "=?", whereArgs);
		return number >= 1 ? true : false;
	}

	public List<IconItem> queryAll() {
		List<IconItem> items = new ArrayList<IconItem>();
		Cursor mCursor = mSQLiteDatabase.query(AREA_TABLE, AREA_COLUMNS, null,
				null, null, null, null);
		while (mCursor.moveToNext()) {
			IconItem item = new IconItem();
			item.setId(mCursor.getInt(COLUMN0));
			item.setTitle(mCursor.getString(COLUMN1));
			item.setFileName(mCursor.getString(COLUMN2));
			item.setDesc(mCursor.getString(COLUMN3));
			items.add(item);
		}
		return items;
	}

}
